package com.keur.money.ui.steps.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.keur.money.R
import com.keur.money.base.adapter.MullaRecyclerViewAdapter
import com.keur.money.base.adapter.MullaViewHolderHelper
import com.keur.money.base.utils.DensityUtils
import com.keur.money.http.HttpHelper
import com.keur.money.utils.decoration.LinearSpacingItemDecoration
import com.keur.money.utils.dialog.StepCommonPopup
import com.keur.money.utils.event.KeurEventType
import org.json.JSONObject


/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/30 10:43
 * @Annotation：流程 - 联系人适配器
 */
class KeurStepPhoneBookAdapter(private val parentView:ViewGroup, recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_mulla_step_phonebook) {

    /**
     * 选择联系电话监听
     */
    private var chooseMobileListsner: IChildItemSelectMobileListener? = null
    fun setOnChildItemSelectMobileListener(listener: IChildItemSelectMobileListener){
        chooseMobileListsner = listener
    }

    override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
        if (position == 0){
            helper.setText(R.id.textItemIndex,String.format(mContext.getString(R.string.step_phonebook_index),"${position+1}"))
        } else {
            helper.setText(R.id.textItemIndex,String.format(mContext.getString(R.string.step_phonebook_index),"${position+1}${mContext.getString(R.string.step_phonebook_above)}"))
        }

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        if (recyclerView.itemDecorationCount == 0){
            val topDecoration = DensityUtils.dp2px(16f)
            recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        }
        val adapter = KeurStepCommonAdapter(recyclerView)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { _, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            when(childModel.optString("action")){
                "CONTACT_GROUP_RELATION" -> {//选择
                    val dataList = arrayListOf<JSONObject>()
                    val dataArray = childModel.optJSONArray("values")
                    if (dataArray != null) {
                        for (i in 0..<dataArray.length()) {
                            dataList.add(dataArray.optJSONObject(i))
                        }
                    }
                    showPopupwindow(childView, dataList) { data ->
                        data?.let {
                            //选择的Other需要设置为空
                            it.put("otherValue","")
                            childModel.put("selectItem", it)
                            adapter.notifyItemChanged(childPosition)

                            val subRemark = "${it.optString("value")}$$${it.optString("duration")}"
                            HttpHelper.addKeurEventAction(String.format(KeurEventType.CLICK.KEUR_CLICK_STEP_XX_SELECT, "${model.optString("stepTitle")}_${childModel.optString("paramName")}_${position+1}"), subRemark)
                        }
                    }
                }
                "CONTACT_GROUP_PHONE" -> {//联系电话
                    chooseMobileListsner?.onChildSelectMobileClick(position,childModel)
                }
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null) {
            for (i in 0..<dataArray.length()){
                val stepItem = dataArray.optJSONObject(i)
                stepItem.put("stepTitle",model.optString("stepTitle"))
                stepItem.put("stepType","PHONEBOOK")
                stepItem.put("stepItemIndex",position+1)
                dataList.add(stepItem)
            }
        }
        adapter.data = dataList

        if (position == itemCount - 1){
            helper.setVisibility(R.id.vLine,View.GONE)
        } else {
            helper.setVisibility(R.id.vLine,View.VISIBLE)
        }
    }

    /**
     * choose Mobile
     */
    interface IChildItemSelectMobileListener{
        fun onChildSelectMobileClick(parentPosition: Int,childModel: JSONObject)
    }

    /**
     * SELECT
     */
    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject?)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        val selectPopup = StepCommonPopup(mContext)
        selectPopup.setWindowWidth(view.width + DensityUtils.dp2px(10f))
        selectPopup.setSelectItemListener(object : StepCommonPopup.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                listener.onChildSelectClick(data)
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        selectPopup.show(view, parentView)
        selectPopup.setBrandList(dataList)
    }
}